package com.carcontrol.rotate;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;


import com.carcontrol.rotate.base.BaseFragment;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

//多媒体
public class Fragment_Fifth extends BaseFragment {
    public static String TAG = "5";

    private FrameLayout v;
    private Context context;
    private Activity activity;

    private long loopTime = 1000;  //时间显示间隔   默认100毫秒
    private boolean isThread = true;
    Thread thread;

    private ImageView iv_screen;  //净化器
    GifImageView iv_route;  //gif控件
    GifDrawable gifDrawable;  //gifDrawable
    Object[] gifObj;    //用于界面离开后gif还能动

    Object[] allParam = new Object[]{

//            new Object[]{"iv", new Rect(373*width/width0-width_d, 151*height/height0-height_d, 777*width/width0, 285*height/height0), R.mipmap.border, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
//            new Object[]{"iv", new Rect(406*width/width0-width_d, 236*height/height0-height_d, 169*width/width0, 132*height/height0), R.mipmap.screen_off, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},

            //开
            new Object[]{"iv", new Rect(418*width/width0-width_d, 258*height/height0-height_d, 103*width/width0, 103*height/height0), R.mipmap.open_off, true, "F411", R.mipmap.open_on},
            //关
            new Object[]{"iv", new Rect(949*width/width0-width_d, 258*height/height0-height_d, 103*width/width0, 103*height/height0), R.mipmap.close_off, true, "F410", R.mipmap.close_on},
//            new Object[]{"iv", new Rect(933*width/width0-width_d, 223*height/height0-height_d, 128*width/width0, 57*height/height0), R.mipmap.gq_off, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
//            new Object[]{"iv", new Rect(933*width/width0-width_d, 319*height/height0-height_d, 128*width/width0, 57*height/height0), R.mipmap.gq2_off, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},

            //多媒体动画
            new Object[]{"gif", new Rect(585*width/width0-width_d, 159*height/height0-height_d, 332*width/width0, 268*height/height0), R.mipmap.screen_gif, false},

//            new Object[]{"tv", new Rect(1080*width/width0-width_d, 220*height/height0-height_d, 0, 0), "1", 30, Color.WHITE},
//            new Object[]{"tv", new Rect(1080*width/width0-width_d, 310*height/height0-height_d, 0, 0), "2", 30, Color.WHITE},

    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = new FrameLayout(getActivity());

        context = v.getContext();

        Log.e("fragment_Fifth===0", v+"==="+width_d+"==="+height_d);

        initView();

//        isThread = true;
//        loopRead();

        return v;
    }

    protected void doAction1(String myDevice, String myStatus){
        Log.e("f5===doAction1", myDevice+"==="+myStatus);

        if(myDevice.equals("f004")){    //f004
            if(myStatus.equals("0001")){  //TODO
                iv_route.setImageResource(R.mipmap.screen_gif);
                gifDrawable.start();

                gifObj[3] = true;
            }else{
                iv_route.setImageResource(R.mipmap.screen_off);
                gifDrawable.stop();

                gifObj[3] = false;
            }
        }
    }

    @Override
    public void onDestroy() {
        Log.e("f5===onDestroy", "===");

        isThread=false;

        if(thread!=null){
            thread.interrupt();
        }

        super.onDestroy();
    }

    private void initView(){
        for (int i=0; i<allParam.length; i++){
            Object[] obj = (Object[])allParam[i];
            Rect rect = (Rect)obj[1];
            String type = (String)obj[0];

            if("tv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                String name = (String)obj[2];
                int textSize = (int)obj[3];
                int textColor = (int)obj[4];
                TextView tv = new TextView(context);
                tv.setText(name);
                tv.setTextSize(textSize);
                tv.setTextColor(textColor);
                v.addView(tv, params);
            }else if("gif".equals(type)){   //GifImageView 加载gif
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                gifObj = obj;
                int imgSrc = (int)obj[2];
                iv_route = new GifImageView(context);
                iv_route.setImageResource(imgSrc);
                iv_route.setScaleType(ImageView.ScaleType.FIT_XY);
                gifDrawable = (GifDrawable) iv_route.getDrawable();

                boolean isOpen = (boolean)obj[3];
                if(isOpen){
//                    iv_screen.setVisibility(View.GONE);
//                    iv_route.setVisibility(View.VISIBLE);
                    gifDrawable.start();
                }else{
//                    iv_screen.setVisibility(View.VISIBLE);
//                    iv_route.setVisibility(View.GONE);
                    iv_route.setImageResource(R.mipmap.screen_off);
                    gifDrawable.stop();
                }

                v.addView(iv_route, params);

            }else if("iv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                int imgSrc = (int)obj[2];
                ImageView iv = new ImageView(context);
                iv.setImageResource(imgSrc);
                iv.setScaleType(ImageView.ScaleType.FIT_XY);
                v.addView(iv, params);

                if(imgSrc==R.mipmap.screen_off){
                    iv_screen = iv;
                }

                boolean isCanClick = (boolean)obj[3];

                if(isCanClick){
                    iv.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            Log.e("onTouch===fifth", event.getAction()+"==="+MainActivity.curPos);

                            if(MainActivity.curPos!=3) return false;

                            int imgId = (int)obj[2];
                            int imgId2 = (int)obj[5];
                            String cmd = (String)obj[4];

                            if(event.getAction()==1){

                                iv.setImageResource(imgId);

                                if(imgId==R.mipmap.open_off){

//                                    iv_route.setImageResource(R.mipmap.screen_gif);
//                                    gifDrawable.start();

//                                    iv_screen.setVisibility(View.GONE);
//                                    iv_route.setVisibility(View.VISIBLE);
//                                    gifObj[3] = true;

//                                    uartJnit.getInstance().sendCmds(toCmd("F411"));
//                                    uartJnit.getInstance().sendCmds(toCmd("D1E0"));

                                }else if(imgId==R.mipmap.close_off){

//                                    iv_route.setImageResource(R.mipmap.screen_off);
//                                    gifDrawable.stop();

//                                    iv_screen.setVisibility(View.VISIBLE);
//                                    iv_route.setVisibility(View.GONE);
//                                    gifObj[3] = false;

//                                    uartJnit.getInstance().sendCmds(toCmd("F410"));
                                }

//                                uartJnit.getInstance().sendCmds(toCmd(cmd));
                                send(cmd);
                            }else{
//                                if(imgId==R.mipmap.open_off){
//                                    iv.setImageResource(R.mipmap.open_on);
//                                }else if(imgId==R.mipmap.close_off){
//                                    iv.setImageResource(R.mipmap.close_on);
//                                }


//                                if(imgId==R.mipmap.open_off){
//                                    iv.setImageResource(R.mipmap.open_on);
////                                    obj[2] = R.mipmap.open_on;
//                                }else if(imgId==R.mipmap.open_on){
//                                    iv.setImageResource(R.mipmap.open_off);
////                                    obj[2] = R.mipmap.open_off;
//                                }else if(imgId==R.mipmap.close_off){
//                                    iv.setImageResource(R.mipmap.close_on);
////                                    obj[2] = R.mipmap.close_on;
//                                }else if(imgId==R.mipmap.close_on){
//                                    iv.setImageResource(R.mipmap.close_off);
////                                    obj[2] = R.mipmap.close_off;
//                                }else if(imgId==R.mipmap.gq_off){
//                                    iv.setImageResource(R.mipmap.gq_on);
////                                    obj[2] = R.mipmap.gq_on;
//                                }else if(imgId==R.mipmap.gq_on){
//                                    iv.setImageResource(R.mipmap.gq_off);
////                                    obj[2] = R.mipmap.gq_off;
//                                }else if(imgId==R.mipmap.gq2_off){
//                                    iv.setImageResource(R.mipmap.gq2_on);
////                                    obj[2] = R.mipmap.gq2_on;
//                                }else if(imgId==R.mipmap.gq2_on){
//                                    iv.setImageResource(R.mipmap.gq2_off);
////                                    obj[2] = R.mipmap.gq2_off;
//                                }

                                iv.setImageResource(imgId2);
                            }

//                            write(bytes); //TODO

                            return true;
                        }
                    });

//                    iv.setOnClickListener(new OnClickListener() {
//
//                        @Override
//                        public void onClick(View v) {
////                        ToastUtil.showMessageApp(context, imgSrc+"==="+obj[2]);
//
//                            int imgId = (int)obj[2];
//                            byte[] bytes = (byte[])obj[4];
//
//                            Log.e("fragment_Fifth===iv", imgId+"==="+bytes.length+"==="+bytes);
//
//
//
//                            /* 串口发送字节 */
////                            write(bytes); //TODO
//
//
//                        }
//                    });
                }

            }
        }
    }
    private void loopRead(){
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (isThread){
                    try {
                        Thread.sleep(loopTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    if(MainActivity.curPos==4){
                        m_myHandler.sendEmptyMessage(1);
                    }


                }

            }
        });
        thread.start();
    }

    protected Handler m_myHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message mes) {
            switch (mes.what) {
                case 0:
                    break;

                case 1:
//                    read(cmd_needle); //TODO

                    Log.e("f5===", "===");

                    break;


                default:
                    break;
            }
            return false;
        }
    });


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

    }

}

